रेंडरिंगhi

recursive types

Translation status: हिन्दी reader-locale proof. Term names and code fences follow the hi pack; supporting prose may still be English.

A विभेद or वर्ग may name itself in its own fields; every backend gives it a finite carrier.

Syntax: विभेद <name> { <Variant> { <name> <field> } } · वर्ग <name> { <name> ∪ शून्य_मान <field> }

Category#

type

Examples#

radix/corpus/recursio/expressio-arbor.fab (canonical · feature)#

A विभेद or वर्ग may name itself in its own fields; every backend gives it a finite carrier.

# =============================================================================
# recursive types — a type may refer to itself in its own fields.
# =============================================================================
#
# What this teaches:
#   • Recursive विभेद — `Expr` variants hold `Expr` operands, so a value
#     is a whole expression tree. `मिलाओ` walks it recursively.
#   • Recursive वर्ग — `Nodus` holds a nullable `Nodus` link, so a value is a
#     linked list ending in `शून्य_मान`.
#   • No keyword, no box type — Faber values alias by reference, so the
#     indirection is implied. Backends that store fields inline (Rust) insert
#     the pointer themselves on the fields that close the cycle.
#
# Common mistakes:
#   • A non-nullable self field on a वर्ग (`Nodus next`) — no value can ever
#     be built, because every node needs another node. End the chain with
#     `∪ शून्य_मान`, or recurse through a विभेद with a leaf variant.
#
# See also: विभेद, वर्ग, मिलाओ, गढ़ो, शून्य_मान
# =============================================================================
#
# EXPECTED OUTPUT:
#   14
#   (2 + (3 * 4))
#   -9
#   6
#   3
#   2
#   15

union Expr {
    Numerus {
        int मान
    },
    Nega {
        Expr interior
    },
    Adde {
        Expr sinister
        Expr dexter
    },
    Multiplica {
        Expr sinister
        Expr dexter
    }
}

fn evalua(Expr e) → int {
    match e {
        case Numerus const मान {
            return मान
        }
        case Nega const interior {
            return 0 - evalua(interior)
        }
        case Adde const sinister, dexter {
            return evalua(sinister) + evalua(dexter)
        }
        case Multiplica const sinister, dexter {
            return evalua(sinister) * evalua(dexter)
        }
    }
}

fn depinge(Expr e) → string {
    match e {
        case Numerus const मान {
            return "§"(मान)
        }
        case Nega const interior {
            return "-§"(depinge(interior))
        }
        case Adde const sinister, dexter {
            return "(§ + §)"(depinge(sinister), depinge(dexter))
        }
        case Multiplica const sinister, dexter {
            return "(§ * §)"(depinge(sinister), depinge(dexter))
        }
    }
}

class Nodus {
    var int मान
    var Nodus ∪ none sequens
}

fn योग(Nodus ∪ none n) → int {
    if n is none {
        return 0
    }
    return n.valor + योग(n.sequens)
}

fn longitudo(Nodus ∪ none n) → int {
    if n is none {
        return 0
    }
    return 1 + longitudo(n.sequens)
}

main {
    const Expr tres ← variant Numerus { मान = 3 }
    const Expr productum ← variant Multiplica { sinister = tres, dexter = variant Numerus { मान = 4 } }
    const Expr arbor ← variant Adde { sinister = variant Numerus { मान = 2 }, dexter = productum }
    print evalua(arbor)
    print depinge(arbor)
    print evalua(variant Nega { interior = variant Adde { sinister = tres, dexter = variant Numerus { मान = 6 } } })

    var Nodus caput ← Nodus { मान = 1, sequens = Nodus { मान = 2, sequens = Nodus { मान = 3, sequens = null } } }
    print योग(caput)
    print longitudo(caput)
    print caput?.sequens?.valor
    caput.sequens ← Nodus { मान = 14, sequens = null }
    print योग(caput)
}

Expected output:

14
(2 + (3 * 4))
-9
6
3
2
15